package com.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

/**
 * @ClassName AnnotationDemo2
 * @Description 通过注解，调用方法
 * @Author 2214361854
 * @Date 2022/3/4 23:01
 * @Version 1.0
 */
public class AnnotationDemo2 {
    @MyTest()
    public void test1(){
        System.out.println("=====test1====");
    }
    @MyTest()
    public void test2(){
        System.out.println("=====test2====");
    }

    public void test3(){
        System.out.println("=====test3====");
    }

    /**
     * 有注解才调用
     * @param args
     */
    public static void main(String[] args) throws Exception, IllegalAccessException {
        AnnotationDemo2 a2 = new AnnotationDemo2();
        Class c = AnnotationDemo2.class;
        Method[] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(MyTest.class)){
                method.invoke(a2);
            }
        }
    }
}


/**
 * @author 2214361854
 */
@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyTest{

}